<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #c {
      border: 1px solid #000;
    }
  </style>
</head>

<body>

  <canvas id="c" width="800" height="800"></canvas>
  <script>
    const context = c.getContext('2d');

    class Rectangle {
      constructor() {
        this.width = 100
        this.height = 100
        this.x = 0
        this.y = 0
        this.maxSpeed = 10 //每帧10个像素
        this.fps = 30
        this.frameInterval = 1000 / this.fps //deltaTime
      }
      update() {
        this.x++
      }
      draw() {
        context.fillRect(this.x, this.y, this.width, this.height);
      }
    }

    let deltaTime = 0
    let lastTime = 0
    let countTime = 0
    const r = new Rectangle()

    function animate(timeStamp) {
      context.clearRect(0, 0, c.width, c.height);


      deltaTime = timeStamp - lastTime
      lastTime = timeStamp
      // console.log(deltaTime) // 13.33ms ，1000ms，刷新75次 16.66刷新60次
      countTime += 13

      if (countTime > r.frameInterval) {
         countTime = 0
         r.update()
      }
      r.draw()


      
     

      requestAnimationFrame(animate)
    }

    animate(0)
  </script>
</body>

</html>